package org.gwtEss.gwtExtensions;

import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Widget;


public class TextBox extends com.google.gwt.user.client.ui.TextBox{
	char[] doNotAllow = {'a', KeyCodes.KEY_DELETE, 'x'};
	
	public TextBox() {
		super();
		init();
	}

	public TextBox(Element element) {
		super(element);
		init();
	}

	private void init() {
		addKeyboardListener(new KeyboardListener(){

			public void onKeyDown(Widget sender, char keyCode, int modifiers) {
				// TODO Auto-generated method stub
				
			}

			public void onKeyPress(Widget sender, char keyCode, int modifiers) {
				// TODO Auto-generated method stub
				
			}

			public void onKeyUp(Widget sender, char keyCode, int modifiers) {
				Window.alert(keyCode + "");
				System.out.println("keycode: ::: " + keyCode);
			}
			
		});
		addKeyDownHandler(new KeyDownHandler(){

			public void onKeyDown(KeyDownEvent event) {
				
				int key = event.getNativeKeyCode();
				System.out.println("event.isMetaKeyDown(): " + (char)key);
				for (int i=0; i< doNotAllow.length; i++){
					System.out.println("--" + (char)doNotAllow[i] + ", key: " + (char)key + ", event.isShiftKeyDown(): " + event.isShiftKeyDown());
					if (doNotAllow[i] == key){
						cancelKey();
						break;
					}
				}
				
			}
			
		});
		
		addKeyUpHandler(new KeyUpHandler(){

			public void onKeyUp(KeyUpEvent event) {
				
			}
			
		});
	}

	public void setDoNotAllowChars(char[] doNotAllow){
		this.doNotAllow = doNotAllow;
	}
}
